﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.Controls;
using ThreeByte.ActiveDeck.Config;
using log4net;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;
using ThreeByte.ActiveDeck.AddIn.Controls.Wizard;
using ThreeByte.ActiveDeck.AddIn.Config.Properties;
namespace ThreeByte.ActiveDeck
{
    public partial class RibbonTab
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(RibbonTab));

        private void RibbonTab_Load(object sender, RibbonUIEventArgs e) {
            
        }

        private void syncAllButton_Click(object sender, RibbonControlEventArgs e) {
            Globals.ThisAddIn.PublishCurrentPresentation();
        }

        private void showConfigurationButton_Click(object sender, RibbonControlEventArgs e) {
            Globals.ThisAddIn.ShowActiveConfigurationTaskPane(true);
        }

        private void hideConfigurationButton_Click(object sender, RibbonControlEventArgs e) {
            Globals.ThisAddIn.ShowActiveConfigurationTaskPane(false);
        }

        private void SetupButton_Click(object sender, RibbonControlEventArgs e) {
            //Open a dialog box
            //Get active Core config
            //TODO: Handle if there is no active presentation
            //Maybe disable the ribbon button
            Presentation pres = Globals.ThisAddIn.Application.ActivePresentation;
            CoreConfig config = Globals.ThisAddIn.GetConfig(pres);

            SetupForm setupForm = new SetupForm(config);
            setupForm.ShowDialog();

        }

        private void InsertFeedbackButton_Click(object sender, RibbonControlEventArgs e) {

            Presentation pres = Globals.ThisAddIn.Application.ActivePresentation;
            CoreConfig config = Globals.ThisAddIn.GetConfig(pres);
            Slide currentSlide = Globals.ThisAddIn.Application.ActiveWindow.View.Slide as Slide;

            SetupUserResponseForm userResponseForm = new SetupUserResponseForm(config);
            userResponseForm.SlideHint = currentSlide;

            System.Windows.Forms.DialogResult result = userResponseForm.ShowDialog();

        }

        private void ClearUserDataButton_Click(object sender, RibbonControlEventArgs e) {
            Globals.ThisAddIn.ClearAllUserDataForCurrentPresentation();
        }

        private void DownloadUserDataButton_Click(object sender, RibbonControlEventArgs e) {
            if (e.Control.Id.Contains("XLS"))
                Globals.ThisAddIn.DownloadCurrentUserData("XLS");
            else 
                Globals.ThisAddIn.DownloadCurrentUserData("XML");
        }

        private Shape _currentlySelectedShape;

        public void ShowInteractionGroup(Shape shape) {
            _currentlySelectedShape = shape;
            interactionGroup.Visible = (_currentlySelectedShape != null);
            if(shape == null) {
                return;
            }
            string pageLink = shape.Tags[PropertyTags.PAGE_LINK];
            if(string.IsNullOrEmpty(pageLink)) {
                RemoveLinkButton.Visible = false;
            } else {
                RemoveLinkButton.Visible = true;
            }
            string layerAction = shape.Tags[PropertyTags.LAYER_ACTION];
            if(string.IsNullOrEmpty(layerAction)) {
                RemoveActionButton.Visible = false;
            } else {
                RemoveActionButton.Visible = true;
            }
        }

        private void RemoveLinkButton_Click(object sender, RibbonControlEventArgs e) {
            //Clear the interaction 
            if(_currentlySelectedShape != null) {
                _currentlySelectedShape.Tags.Delete(PropertyTags.PAGE_LINK);
                RemoveLinkButton.Visible = false;
            } else {
                log.Warn("No currently selected shape");
            }
        }

        private void AddLinkButton_Click(object sender, RibbonControlEventArgs e) {

            if(_currentlySelectedShape == null) {
                log.Warn("No currently selected shape");
                return;
            }

            //Open the choose page navigator
            CoreConfig currentConfig = Globals.ThisAddIn.GetCurrentConfig();
            PageChooserControl pageChooser = new PageChooserControl(currentConfig);
            HostForm newDialog = new HostForm(pageChooser);
            if(newDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                PageConfig selectedPage = pageChooser.SelectedPage;
                _currentlySelectedShape.Tags.Add(PropertyTags.PAGE_LINK, string.Format("{0}", selectedPage.PageId));
            }
            RemoveLinkButton.Visible = true;
        }

        private void pageLinkButton_Click(object sender, RibbonControlEventArgs e) {
            //Choose a new page
            AddLinkButton_Click(AddLinkButton, e);
        }


        //*******************************************************************************
        //Layer Actions
        private void RemoveActionButton_Click(object sender, RibbonControlEventArgs e) {
            if(_currentlySelectedShape != null) {
                _currentlySelectedShape.Tags.Delete(PropertyTags.LAYER_ACTION);
                RemoveActionButton.Visible = false;
            } else {
                log.Warn("No currently selected shape");
            }
        }

        private void ConfigureLayerActionButton_Click(object sender, RibbonControlEventArgs e) {
            if(_currentlySelectedShape == null) {
                log.Warn("No currently selected shape");
                return;
            }

            //Open the choose page navigator
            LayerChooserControl layerChooser = new LayerChooserControl();
            HostForm newDialog = new HostForm(layerChooser);
            if(newDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                NavigationAction theAction = layerChooser.ConfiguredAction;
                _currentlySelectedShape.Tags.Add(PropertyTags.LAYER_ACTION, string.Format("{0}:{1}", theAction.State, theAction.LayerTarget));
            }
            RemoveLinkButton.Visible = true;
        }

        private void layerActionButton_Click(object sender, RibbonControlEventArgs e) {
            ConfigureLayerActionButton_Click(layerActionButton, e);
        }

        private void errorReportButton_Click(object sender, RibbonControlEventArgs e) {
            //Open the Error Report Dialog

            ErrorReportControl reportControl = new ErrorReportControl();
            HostForm newDialog = new HostForm(reportControl);
            if(newDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                try {
                    ThreeByte.ActiveDeck.Service.WebServiceClient.SendErrorReport(reportControl.ReportDetails, Globals.ThisAddIn.GetCurrentConfig().ID, reportControl.LogAttachment);
                } catch {
                    System.Windows.MessageBox.Show("There was a problem submitting the report");
                }
            }

        }

        private void helpButton_Click(object sender, RibbonControlEventArgs e) {

        }

        private void insertGroupResponseButton_Click(object sender, RibbonControlEventArgs e) {
            Presentation pres = Globals.ThisAddIn.Application.ActivePresentation;
            CoreConfig config = Globals.ThisAddIn.GetConfig(pres);
            Slide currentSlide = Globals.ThisAddIn.Application.ActiveWindow.View.Slide as Slide;

            SetupUserResponseForm userResponseForm = new SetupUserResponseForm(config, null, true);
            userResponseForm.SlideHint = currentSlide;

            System.Windows.Forms.DialogResult result = userResponseForm.ShowDialog();
        }

        private void wizardButton_Click(object sender, RibbonControlEventArgs e) {
            //Open the Wizard
            Globals.ThisAddIn.OpenWizard();
        }

       
    }
}
